﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LeSoft.Application.Bussiness;
using LeSoft.Application.Cache;
using LeSoft.Application.Code;
using LeSoft.Application.Entity;
using LeSoft.Util;

namespace LeSoft.Application.Web.Areas.PropertyManage.Controllers
{
    public class CS_RectificationController : MvcControllerBase
    {
        private DataItemDetailBLL dataItemDetailBLL = new DataItemDetailBLL();
        private RectificationBillBLL rectificationBillBLL = new RectificationBillBLL();
        public DecorateBLL decorateBLL = new DecorateBLL();
        public PStructBLL pstructBLL = new PStructBLL();

        //public PStructUserBLL pstructUserBLL = new PStructUserBLL();

        #region 视图

        [HttpGet]
        [HandlerAuthorize(PermissionMode.Enforce)]
        [HandlerAction(OperationType.Visit)]
        public ActionResult Index()
        {
            return View();
        }

        [HttpGet]
        [HandlerAuthorize(PermissionMode.Enforce)]
        [HandlerAction(OperationType.Visit)]
        public ActionResult Form()
        {
            if (Request["SourceId"] != null)
            {
                var data = decorateBLL.GetEntity(Request["SourceId"].ToString());
                var dataPstruct = pstructBLL.GetEntity(data.PStructId);

                //var model = pstructUserBLL.GetList(data.PStructId).FirstOrDefault();
                if (dataPstruct != null)//&& model != null
                {
                    ViewBag.PStruct = dataPstruct.Name;//+ "/" + model.CustomerInfoName
                }

                if (data != null)
                {
                    ViewBag.PStructId = data.PStructId;
                    ViewBag.Cname = data.Cname;
                    ViewBag.Ccode = data.Ccode;
                    ViewBag.ConstructionUnit = data.ConstructionUnit;
                    ViewBag.ConstructionManager = data.ConstructionManager;
                }
            }
            return View();
        }

        #endregion

        #region 获取数据
        public ActionResult GetJson()
        {
            return Content("");
        }
        [HttpGet]
        [HandlerAuthorize(PermissionMode.Ignore)]
        [HandlerAction(OperationType.GetList, "获取整改单列表", "GetPageListJson")]
        public ActionResult GetPageListJson(Pagination pagination, string queryJson)
        {
            var queryParam = queryJson.ToJObject();
            var ids = "";
            var DecorateIds = "";
            if (!queryParam["PStructId"].IsEmpty())
            {
                PStructCache pscache = new PStructCache();
                ids = pscache.GetSonId(queryParam["PStructId"].ToString());
                if (queryParam["SourceType"].IsEmpty() || queryParam["SourceType"].ToString() == "商户装修" || queryParam["SourceType"].ToString() == "所有")
                {
                    var dataDecorate = decorateBLL.GetList(-1, "ALL").Where(t => ids.Contains(t.PStructId));
                    foreach (var item in dataDecorate)
                    {
                        DecorateIds += item.Id + ",";
                    }
                }
            }
            queryParam["ids"] = ids;
            queryParam["DecorateIds"] = DecorateIds;
            queryJson = queryParam.ToJson();
            var data = rectificationBillBLL.GetPageList(pagination, queryJson);
            return Content(data.ToJson());
        }
        [HttpGet]
        [HandlerAuthorize(PermissionMode.Ignore)]
        [HandlerAction(OperationType.GetForm, "获取实体", "GetFormBillJson")]
        public ActionResult GetFormBillJson(string keyValue)
        {
            var Rdata = rectificationBillBLL.GetEntity(keyValue);
            var Ddata = decorateBLL.GetEntity(Rdata.SourceId);
            return Content(Rdata.ToJson());
        }
        #endregion

        #region 提交数据

        [HttpPost]
        [ValidateAntiForgeryToken]
        [AjaxOnly]
        [HandlerAction(OperationType.Create, "保存整改单", "SaveForm")]
        public ActionResult SaveForm(string keyValue, RectificationBillEntity rectificationBillEntity)
        {
            try
            {
                rectificationBillBLL.SaveForm(keyValue, rectificationBillEntity);
                return Success("操作成功。");
            }
            catch (Exception e)
            {
                return Error(e.Message);
            }
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        [AjaxOnly]
        [HandlerAction(OperationType.Delete, "删除整改", "RemoveForm")]
        public ActionResult RemoveForm(string keyValue)
        {
            rectificationBillBLL.RemoveForm(keyValue);
            return Success("操作成功");
        }
        #endregion
    }
}
